//
//  RegistViewController.m
//  TenMinDemo
//
//  Created by Shawn Li on 2021/3/11.
//  Copyright © 2021 CYXiang. All rights reserved.
//

#import "RegistViewController.h"
#import "LanguageSelectTableViewController.h"
#import "CountryCodeSelectViewController.h"
#import "CYXBaseRequest.h"
#import "PicVerCodeView.h"
#import "CheckInput.h"
#import "VerifyCodeBtn.h"
#import "BaseTextField.h"

@interface RegistViewController ()
//@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgTop;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollBgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollBgBottom;
@property (weak, nonatomic) IBOutlet UILabel *errMsg;
@property (weak, nonatomic) IBOutlet VerifyCodeBtn *pvcBtn;
@property (weak, nonatomic) IBOutlet BaseTextField *ivtCodeInput;
@property (weak, nonatomic) IBOutlet BaseTextField *pvcInput;
@property (weak, nonatomic) IBOutlet BaseTextField *cPswInput;
@property (weak, nonatomic) IBOutlet BaseTextField *phoneInput;
@property (weak, nonatomic) IBOutlet UIButton *codeSelectBtn;
@property (weak, nonatomic) IBOutlet BaseTextField *pswInput;
/// <#注释#>
@property (nonatomic, copy) NSString *countryCode;
@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.countryCode = @"86";
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)goToLogOn:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)languageSelect:(id)sender {
    LanguageSelectTableViewController *vc = [[LanguageSelectTableViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setupUI{
    [super setupUI];
    self.view.backgroundColor = LRRGBColor(80, 42, 154);
    [self setMainEditScrollScrollView:self.scrollBgView andScrollViewBottom:self.scrollBgBottom];
    LRWeakSelf(self);
    self.pvcBtn.clickBlock = ^{
//        [weakself getPvc_net];
        [weakself.view endEditing:YES];
        [weakself checkIsRegistered];
        
    };
    [self.pvcBtn configVerifyBtn:60];
    
    self.phoneInput.placeholder = LCLocalString(@"plh_qssjh");
    self.phoneInput.placeholderColor = PlhColor;
    self.pswInput.placeholder = LCLocalString(@"plh_qsrmm");
    self.pswInput.placeholderColor = PlhColor;
    self.cPswInput.placeholder = LCLocalString(@"psw_yzmm");
    self.cPswInput.placeholderColor = PlhColor;
    self.pvcInput.placeholder = LCLocalString(@"plh_qsryzm");
    self.pvcInput.placeholderColor = PlhColor;
    self.ivtCodeInput.placeholder = LCLocalString(@"plh_qssyqm");
    self.ivtCodeInput.placeholderColor = PlhColor;
    
}

- (void)getGvc{
    PicVerCodeView *view = [[PicVerCodeView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT)];
    view.gvcApi = API_getGvc;
    LRWeakSelf(self);
    view.actionBlock = ^(NSInteger index,NSString *gvCode) {
        CYXLog(@"%zd",index);
        if(index == 1){
            [weakself.view endEditing:YES];
            [weakself getPvc_net:gvCode];
        }
    };
    
    [view showInView:self.navigationController.view];
}
- (BOOL)checkPhone{
    if (![CheckInput isPhoneNum:self.phoneInput.text]) {
//        [self showErrorMsg:@"请输入正确的手机号"];
        LCInfoHUD(LCLocalString(@"ts_sjhyw"),1);
        return NO;
    }
//    if (!self.pswInput.text.length) {
////        [self showErrorMsg:@"请输入正确的手机号"];
//        LCInfoHUD(self.pswInput.placeholder,1);
//        return NO;
//    }
    return YES;
}
//- (IBAction)getVerCode:(id)sender {
//    if (![self checkIsPhone]) return;
//    [self checkIsRegistered];
//}

- (void)showErrorMsg:(NSString *)msg{
    if (msg.length) {
        [self.view endEditing:YES];
    }
    self.errMsg.text = msg;
}

- (BOOL)checkIsPhone{
    if (![CheckInput isPhoneNum:self.phoneInput.text]) {
        [self showErrorMsg:@"请输入正确的手机号"];
        return NO;
    }
    return YES;
}

- (void)checkIsRegistered{
    if (![self checkPhone]) return;
    LRLog(@"判断是否注册过");
    NSDictionary *param = @{ @"phone" : self.phoneInput.text };
        
    [CYXBaseRequest getWithUrl:API_isRegistered param:param resultClass:nil success:^(id res) {
        
        if ([res isEqual: RequestErrorCode]) {
            
        }else{
            BOOL isRegisted = [res[@"registered"] boolValue];
            if (isRegisted) {
                LCInfoHUD(LCLocalString(@"ts_sjhyzc"), 2);
            }else{
                [self getGvc];
            }
        }
        
        CYXLog(@"%@",res)
    } failure:^(NSError *error) {
        CYXLog(@"%@",error)
    }];
    
    
}

- (void)getPvc_net:(NSString *)gvCode{
    if (![self checkPhone]) return;
    
    NSDictionary *param = @{@"phone":self.phoneInput.text,@"gvc":gvCode};
    
        [CYXBaseRequest postWithUrl:API_sendRegisterPvc param:param resultClass:nil success:^(id res) {
            if ([res isEqual: RequestErrorCode]) {
                
            }else{
                LCInfoHUD(@"驗證碼已發送", 1);
                [self.pvcBtn beginRun];
            }
            
            CYXLog(@"%@",res)
        } failure:^(NSError *error) {
            CYXLog(@"%@",error)
        }];
}

- (BOOL)checkInput{
    if (![CheckInput isPhoneNum:self.phoneInput.text]) {
//        [self showErrorMsg:@"请输入正确的手机号"];
        LCInfoHUD(LCLocalString(@"ts_sjhyw"),1);
        return NO;
    }
    if (!self.pswInput.text.length) {
//        [self showErrorMsg:@"请输入正确的手机号"];
        LCInfoHUD(self.pswInput.placeholder,1);
        return NO;
    }
    if (self.pswInput.text.length < 8 || self.pswInput.text.length > 20) {
//        [self showErrorMsg:@"请输入正确的手机号"];
        LCInfoHUD(LCLocalString(@"mmcwts"),1);
        return NO;
    }
    if (![self.pswInput.text isEqualToString:self.cPswInput.text]) {
//        [self showErrorMsg:@"请输入正确的手机号"];
        LCInfoHUD(LCLocalString(@"lcmmbt"),1);
        return NO;
    }
    return YES;
}


- (IBAction)regisAction:(id)sender {
    
    if (![self checkInput]) return;
    
    NSMutableDictionary *param = @{
        @"phone":[NSString stringWithFormat:@"%@",self.phoneInput.text],
        @"code":self.pvcInput.text,
        @"password":self.pswInput.text,
        @"source":@"ios",
        @"countryCode":self.countryCode,
    }.mutableCopy;
    if (self.ivtCodeInput.text.length) {
        param[@"invitationCode"] = self.ivtCodeInput.text;
    }
    
        [CYXBaseRequest postWithUrl:API_register param:param resultClass:nil success:^(id res) {
            if ([res isEqual: RequestErrorCode]) {
                
            }else{
                LCInfoHUD(LCLocalString(@"ts_zccg"), 1);
                [self.navigationController popViewControllerAnimated:YES];
            }
            
            CYXLog(@"%@",res)
        } failure:^(NSError *error) {
            CYXLog(@"%@",error)
        }];
}

- (IBAction)countrySelectAction:(id)sender {
    CountryCodeSelectViewController *vc = LoadVCFormXib(CountryCodeSelectViewController);
    vc.backBlock = ^(NSDictionary * _Nonnull countryInfo) {
        self.countryCode = countryInfo[@"tel"];
        [self.codeSelectBtn setTitle:[NSString stringWithFormat:@"+%@",self.countryCode] forState:(UIControlStateNormal)];
    };
    //设置模态视图弹出样式
    vc.modalPresentationStyle = UIModalPresentationOverFullScreen;

    [self presentViewController:vc animated:YES completion:nil];
}



@end
